﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmUserAndBranch : Form
    {
        string _RegionCode = "";
        BranchClient _wcfBranch;
        DataTable _dsBranch = null;
        UserClient _wcfUser;

        public frmUserAndBranch(string strUsername)
        {
            InitializeComponent();
            _wcfBranch = Service.Branch();
            _wcfUser = Service.User();
            
            BuildVoucherGridUnmanage();
            BuildVoucherGridManage();

            lblHoVaTen.Visible = toolStripSeparator2.Visible = false;
            UISetting.LoadRegion(ref cboRegion);
            cboRegion.ComboBox.SelectedValue = 0;

            if (strUsername != "")
            {
                txtUsername.Text = strUsername;
                txtUsername.Focus();
                btnSearchUsername_Click(null, null);
            }
            else
            {
                cboRegion_SelectedIndexChanged(null, null);
                txtUsername.Focus();
            }
        }

        private void BuildVoucherGridManage()
        {
            UISetting.AddColumn(ref dtgBranchUnmanage, "Branch_Code", "Mã đơn vị", "", 80, true, true);
            UISetting.AddColumn(ref dtgBranchUnmanage, "Branch_Name", "Tên đơn vị", "", 320, true, true);
        }

        private void BuildVoucherGridUnmanage()
        {
            UISetting.AddColumn(ref dtgBranchmanage, "Branch_Code", "Mã đơn vị", "", 80, true, true);
            UISetting.AddColumn(ref dtgBranchmanage, "Branch_Name", "Tên đơn vị", "", 320, true, true);
        }

        private void cboRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadBranchUnmanage();
        }

        private void LoadBranchUnmanage()
        {
            _RegionCode = cboRegion.ComboBox.SelectedValue.ToString() != "0" ? cboRegion.ComboBox.SelectedValue.ToString() : "";
            string username = txtUsername.Text.Trim();
            string branchName = txtBranchName.Text.Trim();
            LoadUserBranchList(_RegionCode,branchName, username, 0);
        }

        private void btnSearchUsername_Click(object sender, EventArgs e)
        {
            txtBranchName.Text = "";
            if (txtUsername.Text.Trim() != "")
            {
                lblHoVaTen.Visible = toolStripSeparator2.Visible = true;
                UserInfo userInfo = _wcfUser.User_GetUserInfo(txtUsername.Text.Trim());
                if (userInfo == null)
                {
                    lblHoVaTen.Text = "Tài khoản không tồn tại!";
                    pnlTaiKhoanDonVi.Enabled = false;
                }
                else
                {
                    lblHoVaTen.Text = userInfo.FullName;
                    cboRegion.ComboBox.SelectedValue = userInfo.RegionCode;
                    LoadUserBranchList(userInfo.RegionCode,"", txtUsername.Text.Trim(), 1);
                    pnlTaiKhoanDonVi.Enabled = true;
                }
            }
            else
            {
                lblHoVaTen.Visible = toolStripSeparator2.Visible = false;
                cboRegion.ComboBox.SelectedValue = 0;
                cboRegion_SelectedIndexChanged(null, null);
                txtUsername.Focus();
                dtgBranchmanage.Rows.Clear();
                pnlTaiKhoanDonVi.Enabled = true;
            }
        }

        private void LoadUserBranchList(string regioncode,string branchname, string username,int typemanage)
        {
            DataSet _dsBranch = _wcfUser.UserBranch_GetListManage(regioncode,branchname, username, typemanage);
            if (_dsBranch.Tables.Count == 0)
                return;

            //Load danh sách đơn vị không quản lý
            if (typemanage == 0)
            {
                dtgBranchUnmanage.Rows.Clear();
                for (int i = 0; i < _dsBranch.Tables[0].Rows.Count; i++)
                {
                    dtgBranchUnmanage.Rows.Insert(i, 1);
                    dtgBranchUnmanage.Rows[i].Cells["Branch_Code"].Value = _dsBranch.Tables[0].Rows[i]["Branch_Code"].ToString();
                    dtgBranchUnmanage.Rows[i].Cells["Branch_Name"].Value = _dsBranch.Tables[0].Rows[i]["Branch_Name"].ToString();
                }
            }
            else if (typemanage == 1)
            {
                dtgBranchmanage.Rows.Clear();
                for (int i = 0; i < _dsBranch.Tables[0].Rows.Count; i++)
                {
                    dtgBranchmanage.Rows.Insert(i, 1);
                    dtgBranchmanage.Rows[i].Cells["Branch_Code"].Value = _dsBranch.Tables[0].Rows[i]["Branch_Code"].ToString();
                    dtgBranchmanage.Rows[i].Cells["Branch_Name"].Value = _dsBranch.Tables[0].Rows[i]["Branch_Name"].ToString();
                }
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (dtgBranchUnmanage.SelectedRows.Count > 0)
            {
                string Branch_Code = dtgBranchUnmanage.SelectedRows[0].Cells["Branch_Code"].Value.ToString();
                string Username = txtUsername.Text.Trim();

                int retVal = _wcfUser.UserBranch_Create(Username, Branch_Code);

                LoadUserBranchList(_RegionCode,txtBranchName.Text.Trim(), Username, 1);
                LoadUserBranchList(_RegionCode,txtBranchName.Text.Trim(), Username, 0);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dtgBranchUnmanage.SelectedRows.Count > 0)
            {
                string Branch_Code = dtgBranchmanage.SelectedRows[0].Cells["Branch_Code"].Value.ToString();
                string Username = txtUsername.Text.Trim();

                _wcfUser.UserBranch_Delete(Username, Branch_Code);

                LoadUserBranchList(_RegionCode, txtBranchName.Text.Trim(), Username, 1);
                LoadUserBranchList(_RegionCode, txtBranchName.Text.Trim(), Username, 0);
            }
        }

        private void txtUsername_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnSearchUsername_Click(null, null);
        }

        private void lblHoVaTen_Click(object sender, EventArgs e)
        {

        }

        private void btnLocDonVi_Click(object sender, EventArgs e)
        {
            _RegionCode = cboRegion.ComboBox.SelectedValue.ToString() != "0" ? cboRegion.ComboBox.SelectedValue.ToString() : "";
            LoadUserBranchList(_RegionCode, txtBranchName.Text.Trim(),"", 0);
        }

        private void txtBranchName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLocDonVi_Click(null, null);
        }
    }
}
